package com.gwq.controller;

import com.gwq.base.ResponseData;
import com.gwq.common.FileCheck;
import com.gwq.entity.Message;
import com.gwq.entity.MessageReply;
import com.gwq.entity.SysRole;
import com.gwq.entity.SysUser;
import com.gwq.param.MessageParam;
import com.gwq.param.MessageGiveLikeAddParam;
import com.gwq.service.MessageService;
import com.gwq.service.SysRoleService;
import com.gwq.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.rmi.ServerException;
import java.security.Principal;
import java.util.Map;

/**
 * <p>
 *  留言管理
 * </p>
 *
 * @author gwq
 * @since 2022-06-07
 */

@Api(description = "留言管理")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;


    @ApiOperation(value="留言新增")
    @PostMapping
    public ResponseData save(@Validated @RequestBody Message message, Principal principal){
        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        if(sysUser!=null){
            message.setCreatedBy(sysUser.getId());
            messageService.addMessage(message);
        }
        return ResponseData.success("新增成功！",true);
    }

    @ApiOperation(value="留言修改")
    @PutMapping
    public ResponseData update(@RequestBody Message message, Principal principal){
        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        if(sysUser!=null) {
            message.setCreatedBy(sysUser.getId());
            messageService.updateMessage(message);
        }
        return ResponseData.success("修改成功！",true);
    }

    @ApiOperation(value="留言查询")
    @GetMapping("/{id}")
    public ResponseData getByMessageId(@PathVariable String id){
        return ResponseData.success("查询成功！",messageService.getByMessageId(id));
    }

    @ApiOperation(value="留言删除")
    @PostMapping("/delete")
    public ResponseData deleteMessage(@RequestBody String[] ids, Principal principal){

        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        if(sysUser!=null){
            SysRole sysRole = sysRoleService.getByUserId(sysUser.getId());
            if(sysRole!=null){
                messageService.deleteMessage(ids);
            }else{
                return ResponseData.success("没有权限删除");
            }
        }else{
            return ResponseData.success("没有权限删除");
        }
        return ResponseData.success("删除成功",true);
    }


    @ApiOperation(value="留言列表查询")
    @GetMapping("/page")
    public ResponseData list(MessageParam messageParam, Principal principal){
        return ResponseData.success("查询成功！",messageService.pageMessage(messageParam,principal));
    }

    @ApiOperation(value="我的留言")
    @GetMapping("/myPage")
    public ResponseData myPage(MessageParam messageParam, Principal principal){
        return ResponseData.success("查询成功！",messageService.myPage(messageParam,principal));
    }


    //上传文件
    @ApiOperation(value="上传文件")
    @PostMapping("/uploadPicture")
    @FileCheck(typeCheckErrorMsg = "不支持的文件格式", supportedSuffixes = {"png", "jpg","jpeg","doc","docx","xls","xlsx"}, type = FileCheck.CheckType.SUFFIX)
    public ResponseData uploadPicture(MultipartFile verticalPicture) throws ServerException {
        return ResponseData.success(messageService.uploadPicture(verticalPicture));

    }

    @ApiOperation(value="回复帖子")
    @PostMapping("/reply")
    public ResponseData addMessageReply(@RequestBody MessageReply messageReply, Principal principal){
        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        messageReply.setCreatedBy(sysUser.getId());
        messageService.messageAddReply(messageReply,principal);

        return ResponseData.success("回复成功！");
    }

    //点赞帖子
    @ApiOperation(value="点赞帖子")
    @PostMapping("/giveLike")
    public ResponseData giveLike(@RequestBody MessageGiveLikeAddParam param, Principal principal) {
        messageService.addAndUpdateGiveLike(param,principal);
        return ResponseData.success("点赞成功！");
    }

    //查看评论分页
    @ApiOperation(value="查看评论分页")
    @PostMapping("/getRemarkList")
    public ResponseData getRemark(@RequestBody MessageGiveLikeAddParam param, Principal principal) {
        return ResponseData.success(messageService.getRemarkPage(param,principal));
    }


    //评论帖子
    @ApiOperation(value="评论帖子")
    @PostMapping("/remarkInvitation")
    public ResponseData remarkInvitation(@RequestBody MessageGiveLikeAddParam param, Principal principal) {
        messageService.addAndUpdateRemark(param,principal);
        return ResponseData.success("评论成功！");
    }

    //点赞评论
    @ApiOperation(value="点赞评论")
    @PostMapping("/giveLikeRemark")
    public ResponseData giveLikeRemark(@RequestBody MessageGiveLikeAddParam param, Principal principal) {
        messageService.giveLikeRemark(param,principal);
        return ResponseData.success("点赞成功!");
    }
}
